package com.game.server.db.dao;

import com.game.majiang.config.LogManager;
import com.game.server.db.Parameter;
import com.game.server.db.ParameterType;
import com.game.server.db.SqlHelper;
import com.game.server.db.entity.XyClubChangesQuota;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import javax.sql.rowset.CachedRowSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.sql.Types;
import java.text.SimpleDateFormat;
import java.util.Date;

public class XyClubChangesQuotaDao {

    //添加[俱乐部的用户额度变动]记录
    public static void insertInto(XyClubChangesQuota quota) {
        //加入创建时间
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date1=formatter.format(date);
        quota.setCreatedate(Timestamp.valueOf(date1));

        String sql = "INSERT INTO xy_club_changes_quota(cid,me_uid,to_uid,type,quota,residue,roomkey,pid,createdate) VALUES (?,?,?,?,?,?,?,?,?);";
        Parameter p1 = new Parameter(Types.INTEGER, quota.getCid(), ParameterType.Input);
        Parameter p2 = new Parameter(Types.INTEGER, quota.getMe_uid(), ParameterType.Input);
        Parameter p3 = new Parameter(Types.INTEGER, quota.getTo_uid(), ParameterType.Input);
        Parameter p4 = new Parameter(Types.INTEGER, quota.getType(), ParameterType.Input);
        Parameter p5 = new Parameter(Types.INTEGER, quota.getQuota(), ParameterType.Input);
        Parameter p6 = new Parameter(Types.INTEGER, quota.getResidue(), ParameterType.Input);
        Parameter p7 = new Parameter(Types.BIGINT, quota.getRoomkey(), ParameterType.Input);
        Parameter p8 = new Parameter(Types.INTEGER, quota.getPid(), ParameterType.Input);
        Parameter p9 = new Parameter(Types.TIMESTAMP, quota.getCreatedate(), ParameterType.Input);

        SqlHelper.execute(sql, p1, p2, p3, p4,p5,p6,p7,p8,p9);
    }

    public static JSONObject findPage(int limit, int page, int clubid, int uid,int type) {
        JSONObject jsonObject = new JSONObject();
        JSONArray array = new JSONArray();
        String where = "";
        if (type!=0){
            where = " and type="+type+" ";
        }
        if (type==-1){
            where = " and type!=5 ";
        }
        String countSql = "select count(id) from xy_club_changes_quota where cid=" + clubid + " and to_uid = " + uid + ""+ where;
        String sql = "select me_uid,type,quota,residue,createdate,roomkey from xy_club_changes_quota where cid=" + clubid + " and to_uid = " + uid+ where + " ORDER BY id desc LIMIT ?," + limit;
        Object scalar = SqlHelper.execScalar(countSql);
        int sumCount = Integer.parseInt(scalar.toString());
        int sumPage = (int) Math.ceil((double) sumCount/limit);
        page = page<1?1:page>sumPage?sumPage:page;
        page = page<1?1:page;
        Parameter p1 = new Parameter(Types.INTEGER, ((page-1)*limit), ParameterType.Input);
        CachedRowSet resultSet = SqlHelper.getResultSet(sql,p1);
        jsonObject.put("page",page);
        jsonObject.put("allpage",sumPage);
        jsonObject.put("allcount",sumCount);
        try {
            while (resultSet.next()) {
                JSONObject jsonData = new JSONObject();
                jsonData.put("meUid", resultSet.getInt(1));
                jsonData.put("type",resultSet.getInt(2));
                jsonData.put("quota",resultSet.getInt(3));
                jsonData.put("residue",resultSet.getInt(4));
                jsonData.put("createdate",resultSet.getTimestamp(5).getTime());
                jsonData.put("roomkey",resultSet.getLong(6));
                array.add(jsonData);
            }
        } catch (SQLException e) {
            LogManager.getInstance().errorLog(e);
        }
        jsonObject.put("array",array);
        return jsonObject;
    }
}
